package com.wzk.travelspringboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.Blog;
import com.wzk.travelspringboot.service.BlogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author wangzikang
 */
@Slf4j
@RestController
@RequestMapping("/admin/blog")
@RequiredArgsConstructor
@Validated
public class AdminBlogController {

    private final BlogService blogService;

    /**
     * 分页查询所有博客
     */
    @GetMapping("/list")
    public Result listBlogs(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Blog> page = new Page<>(pageNum, pageSize);
        Page<Blog> blogPage = blogService.page(page);
        return Result.success(blogPage);
    }

    /**
     * 根据ID查询博客详情
     */
    @GetMapping("/detail/{id}")
    public Result getBlogDetail(@PathVariable("id") @NotNull(message = "博客ID不能为空") Long id) {
        Blog blog = blogService.getById(id);
        if (blog == null) {
            return Result.failed("博客不存在");
        }
        return Result.success(blog);
    }

    /**
     * 根据ID删除博客
     */
    @DeleteMapping("/remove/{id}")
    public Result deleteBlog(@PathVariable("id") @NotNull(message = "博客ID不能为空") Long id) {
        return blogService.delByBlogId(id);
    }

    /**
     * 根据用户ID查询博客
     */
    @GetMapping("/user/{userId}")
    public Result getBlogsByUserId(@PathVariable("userId") @NotNull(message = "用户ID不能为空") Long userId) {
        return blogService.queryBlogByUserId(userId);
    }

    /**
     * 根据内容模糊查询博客
     */
    @GetMapping("/search")
    public Result searchBlogByContent(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam String content) {
        Page<Blog> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Blog::getContent, content);
        Page<Blog> blogPage = blogService.page(page, queryWrapper);
        return Result.success(blogPage);
    }

    /**
     * 审核博客（设置为显示或隐藏）
     */
    @PostMapping("/audit/{id}/{status}")
    public Result auditBlog(
            @PathVariable("id") @NotNull(message = "博客ID不能为空") Long id,
            @PathVariable("status") @NotNull(message = "状态不能为空") Integer status) {
        Blog blog = new Blog();
        blog.setId(id);
        // 使用isDelete字段，0表示显示，1表示隐藏/删除
        blog.setIsDelete(status == 0 ? 0 : 1);
        boolean success = blogService.updateById(blog);
        return success ? Result.success("审核博客成功") : Result.failed("审核博客失败");
    }
} 